//
//  FCSettingFingerVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/4/19.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCSettingFingerVC.h"
#import "FCFingerVerifyPinVC.h"
@interface FCSettingFingerVC (){
    UIView     *_viewNav;
    
}

/* <#Annotation#> */
@property (strong, nonatomic) UISwitch *fingerSwitch;
@property (nonatomic,assign) BOOL switchStates;//开关的状态
@end

@implementation FCSettingFingerVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kWhiteColor;
   [FCLocalUserHelper saveIsMainVC:@"0"];
    [self setNav];
    NSString *str = [FCLocalUserHelper getPasswordFingerToken];
    
    if (!kStringIsEmpty(str))
    {
        self.switchStates = 1;
    }else
    {
        self.switchStates = 0;
    }
    [self setupUI];
}
#pragma mark-设置UI
-(void)setupUI{
  
    //指纹图片
    UIImageView *im = [UIImageView new];
    [self.view addSubview:im];
    [im mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(_viewNav.mas_bottom).offset(160*KScreenHeightScale1334);
        make.size.mas_equalTo(CGSizeMake(80, 80));
    }];
    im.image = [UIImage imageNamed:@"setfinger"];
    //uiview
    UIView *view = [UIView new];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(im.mas_bottom).offset(200*KScreenHeightScale1334);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(150*KScreenHeightScale1334);
    }];
    view.backgroundColor = kWhiteColor;
    //finger
    UILabel *fingerLabel = [UILabel new];
    [view addSubview:fingerLabel];
    [fingerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(8);
    }];
    fingerLabel.text = Localized(@"指纹");
    fingerLabel.font = Kfont(32);
    //提示语
    UILabel *hintLabel = [UILabel new];
    [view addSubview:hintLabel];
    [hintLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(fingerLabel.mas_bottom).offset(20*KScreenHeightScale1334);
        make.width.mas_equalTo(500*KScreenWidthScale750);
    }];
    hintLabel.text = Localized(@"开启后，转账或消费时，可通过验证指纹快速完成付款／登入");
    hintLabel.font = Kfont(28);
    hintLabel.textColor = kGrayColor;
    hintLabel.numberOfLines = 2;
    //开关
    UISwitch *fingerSwitch = [UISwitch new];
    self.fingerSwitch = fingerSwitch;
    [view addSubview:fingerSwitch];
    [fingerSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(0);
    }];
   
    
        fingerSwitch.on = self.switchStates;

                           
    
    [fingerSwitch addTarget:self action:@selector(openFinger:) forControlEvents:UIControlEventValueChanged];
    
}
#pragma mark-开启指纹
-(void)openFinger:(UISwitch*)sender{
    
    if (sender.isOn) {
        
        //验证pin
        
        FCFingerVerifyPinVC *vc = [FCFingerVerifyPinVC new];
        [self.navigationController pushViewController:vc animated:YES];
        [vc setPrintStatus:^(BOOL isOpen) {
             dispatch_async(dispatch_get_main_queue(), ^{
               sender.on = isOpen;
             });
        }];
    }else{
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:Localized(@"提示") message:Localized(@"你确定要关闭指纹功能吗!") preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *action = [UIAlertAction actionWithTitle:Localized(@"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //移除指纹
            [self removeFingerPrint];
        }];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:Localized(@"取消") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.fingerSwitch.on = YES;
        }];
        [alertController addAction:action];
        [alertController addAction:action2];
        [self presentViewController:alertController animated:YES completion:nil];
       
    }
    
    
}
-(void)removeFingerPrint{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *arrM =[FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",arrM[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",arrM[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    NSString *xidStr = [FCLocalUserHelper getXidStr];
    [dict setObject:xidStr forKey:@"XID"];
    NSString *url = Remove_finger_info_URL;
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                
                [self showAlert:Localized(@"指纹功能已关闭")];
                [FCLocalUserHelper cleanPasswordFingerToken];
            }
        }
        
        
    } faile:^(NSError *error) {
        
        
        
    }];
    
    
    
}
#pragma mark-设置导航栏
-(void)setNav{
    self.navigationController.navigationBarHidden = YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(88);
            }else{
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(64);
                make.top.mas_equalTo(0);
            }
        }else{
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(64);
            make.top.mas_equalTo(0);
        }
        
    }];
    _viewNav.backgroundColor = kRedOrangeColor;
    
    UIButton *leftBtn = [UIButton new];
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*KScreenWidthScale750);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [leftBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //返回
    UILabel *backLabel = [UILabel new];
    
    [_viewNav addSubview:backLabel];
    [backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_viewNav);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
    }];
    backLabel.text = Localized(@"指纹设置");
    backLabel.textColor = [UIColor whiteColor];
    backLabel.font = KOverstrikingfont(36);
    
    //事件
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)leftBtnClick{
    
     [self.navigationController popViewControllerAnimated:YES];
}

@end
